package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.AdminRegiomService;
import com.cskaoyan.market.service.impl.AdminRegionServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.RegionListVo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @author:yanzijun
 * @time:2024/06/20/21:18
 */
@WebServlet("/admin/region/list")
public class AdminRegionListController extends HttpServlet {
    AdminRegiomService adminRegiomService = new AdminRegionServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        List<RegionListVo> region = (List<RegionListVo>) servletContext.getAttribute("region");
        if (region == null) {
            region = adminRegiomService.list();
            servletContext.setAttribute("region", region);
        }
        Object o = ResponseUtil.okList(region);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);

    }
}
